<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>智慧教室控制中心 - By: 挨踢魔君</title>
    <script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Orbitron:wght@400;500;600&family=Roboto:wght@300;400;500&display=swap" rel="stylesheet">
    <style>
        :root {
            --primary: #0a8dea;
            --primary-dark: #0066cc;
            --secondary: #00c6ff;
            --dark-bg: #0a1929;
            --card-bg: #132f4c;
            --text-primary: #ffffff;
            --text-secondary: #a0c5e8;
            --success: #00c853;
            --warning: #ff9100;
            --danger: #ff1744;
            --border-radius: 16px;
            --transition: all 0.3s ease;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Roboto', sans-serif;
            background: linear-gradient(135deg, var(--dark-bg) 0%, #102a43 100%);
            color: var(--text-primary);
            min-height: 100vh;
            padding: 20px;
            line-height: 1.6;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
        }

        header {
            text-align: center;
            margin-bottom: 30px;
            padding: 20px 0;
            border-bottom: 1px solid rgba(10, 141, 234, 0.3);
        }

        h1 {
            font-family: 'Orbitron', sans-serif;
            font-weight: 600;
            font-size: 2.5rem;
            background: linear-gradient(to right, var(--primary), var(--secondary));
            -webkit-background-clip: text;
            background-clip: text;
            color: transparent;
            margin-bottom: 10px;
            letter-spacing: 1px;
        }

        .subtitle {
            color: var(--text-secondary);
            font-size: 1.1rem;
        }

        .dashboard {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 25px;
            margin-bottom: 30px;
        }

        .card {
            background: var(--card-bg);
            border-radius: var(--border-radius);
            padding: 25px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
            transition: var(--transition);
            border: 1px solid rgba(10, 141, 234, 0.1);
        }

        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.4);
            border-color: rgba(10, 141, 234, 0.3);
        }

        .card-header {
            display: flex;
            align-items: center;
            margin-bottom: 20px;
        }

        .card-icon {
            width: 50px;
            height: 50px;
            background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-right: 15px;
        }

        .card-icon i {
            font-size: 1.5rem;
            color: white;
        }

        .card-title {
            font-size: 1.4rem;
            font-weight: 500;
        }

        .sensor-value {
            font-size: 2.5rem;
            font-weight: 700;
            margin: 10px 0;
            font-family: 'Orbitron', sans-serif;
            background: linear-gradient(to right, var(--primary), var(--secondary));
            -webkit-background-clip: text;
            background-clip: text;
            color: transparent;
        }

        .temperature-high {
            background: linear-gradient(to right, var(--danger), #ff5252);
            -webkit-background-clip: text;
            background-clip: text;
            color: transparent;
            text-shadow: 0 0 10px rgba(255, 23, 68, 0.5);
        }

        .sensor-unit {
            font-size: 1.2rem;
            color: var(--text-secondary);
            margin-left: 5px;
        }

        .sensor-status {
            display: flex;
            align-items: center;
            margin-top: 10px;
            font-size: 0.9rem;
        }

        .status-dot {
            width: 8px;
            height: 8px;
            border-radius: 50%;
            margin-right: 8px;
        }

        .status-normal {
            background-color: var(--success);
        }

        .status-warning {
            background-color: var(--warning);
        }

        .status-danger {
            background-color: var(--danger);
            box-shadow: 0 0 8px var(--danger);
        }

        .controls {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 25px;
        }

        .control-button {
            background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
            border: none;
            border-radius: 12px;
            color: white;
            padding: 16px 25px;
            font-size: 1.1rem;
            font-weight: 500;
            cursor: pointer;
            transition: var(--transition);
            display: flex;
            align-items: center;
            justify-content: center;
            box-shadow: 0 5px 15px rgba(10, 141, 234, 0.3);
        }

        .control-button i {
            margin-right: 10px;
            font-size: 1.3rem;
        }

        .control-button:hover {
            transform: translateY(-3px);
            box-shadow: 0 8px 20px rgba(10, 141, 234, 0.5);
        }

        .control-button:active {
            transform: translateY(1px);
        }

        .beep-button {
            background: linear-gradient(135deg, var(--warning) 0%, #ff6d00 100%);
            box-shadow: 0 5px 15px rgba(255, 145, 0, 0.3);
        }

        .beep-button:hover {
            box-shadow: 0 8px 20px rgba(255, 145, 0, 0.5);
        }

        .status-indicator {
            display: inline-block;
            width: 12px;
            height: 12px;
            border-radius: 50%;
            margin-left: 10px;
        }

        .status-connected {
            background-color: var(--success);
            box-shadow: 0 0 8px var(--success);
        }

        .status-disconnected {
            background-color: #ccc;
        }

        footer {
            text-align: center;
            margin-top: 50px;
            padding: 20px 0;
            color: var(--text-secondary);
            font-size: 0.9rem;
            border-top: 1px solid rgba(10, 141, 234, 0.3);
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            h1 {
                font-size: 2rem;
            }
            
            .dashboard, .controls {
                grid-template-columns: 1fr;
            }
            
            .sensor-value {
                font-size: 2rem;
            }
            
            .card {
                padding: 20px;
            }
        }

        @media (max-width: 480px) {
            body {
                padding: 15px;
            }
            
            h1 {
                font-size: 1.8rem;
            }
            
            .card-title {
                font-size: 1.2rem;
            }
            
            .sensor-value {
                font-size: 1.8rem;
            }
            
            .control-button {
                padding: 14px 20px;
                font-size: 1rem;
            }
        }

        /* 连接状态动画 */
        @keyframes pulse {
            0% { opacity: 1; }
            50% { opacity: 0.5; }
            100% { opacity: 1; }
        }

        .pulse {
            animation: pulse 2s infinite;
        }

        /* 数据加载动画 */
        .loading {
            color: var(--text-secondary) !important;
            display: inline-block;
            position: relative;
        }

        .loading::after {
            content: "";
            position: absolute;
            width: 100%;
            height: 2px;
            bottom: -4px;
            left: 0;
            background: linear-gradient(to right, var(--primary), var(--secondary));
            animation: loading 1.5s ease-in-out infinite;
        }

        @keyframes loading {
            0% { transform: scaleX(0); transform-origin: left; }
            50% { transform: scaleX(1); transform-origin: left; }
            51% { transform: scaleX(1); transform-origin: right; }
            100% { transform: scaleX(0); transform-origin: right; }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>智慧教室控制中心</h1>
            <p class="subtitle">实时监控与设备控制 <span class="status-indicator status-connected pulse" id="connectionStatus"></span></p>
        </header>
        
        <div class="dashboard">
            <div class="card">
                <div class="card-header">
                    <div class="card-icon">
                        <i class="fas fa-temperature-high"></i>
                    </div>
                    <h2 class="card-title">温度传感器</h2>
                </div>
                <div class="card-content">
                    <div class="sensor-value"><span id="temperature" class="loading">Loading...</span><span class="sensor-unit">°C</span></div>
                    <div class="sensor-status">
                        <span class="status-dot status-normal" id="tempStatus"></span>
                        <span id="tempStatusText">温度正常</span>
                    </div>
                </div>
            </div>
            
            <div class="card">
                <div class="card-header">
                    <div class="card-icon">
                        <i class="fas fa-tint"></i>
                    </div>
                    <h2 class="card-title">湿度传感器</h2>
                </div>
                <div class="card-content">
                    <div class="sensor-value"><span id="humidity" class="loading">Loading...</span><span class="sensor-unit">%RH</span></div>
                    <div class="sensor-status">
                        <span class="status-dot status-normal" id="humidStatus"></span>
                        <span id="humidStatusText">湿度正常</span>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="controls">
            <button class="control-button" id="LightOn">
                <i class="fas fa-lightbulb"></i> 开 灯
            </button>
            
            <button class="control-button beep-button" id="BeepOn">
                <i class="fas fa-bell"></i> 报 警
            </button>
        </div>
    </div>
    
    <footer>
        <p>智慧教室系统 &copy; 2025 By: 挨踢魔君</p>
    </footer>

    <script>
        function getQueryParam(param) {
            const url = window.location.href;
            const regex = new RegExp('[?&]' + param + '=([^&#]*)');
            const results = regex.exec(url);
            if (results === null) {
                return null;
            } else {
                return decodeURIComponent(results[1]) || '';
            }
        }



        // 原有的MQTT连接代码保持不变
        const deviceId = getQueryParam('did') || 'itmojun'; // 默认设备ID
        const clientId = 'mqttjs_' + Math.random().toString(16).substr(2, 8);
        const host = 'ws://itmojun.com:8083/mqtt';
        const options = {
            keepalive: 60,
            clientId: clientId,
            protocolId: 'MQTT',
            protocolVersion: 4,
            clean: true,
            reconnectPeriod: 1000,
            connectTimeout: 30 * 1000,
            will: {
                topic: 'WillMsg',
                payload: 'Connection Closed abnormally..!',
                qos: 0,
                retain: false
            },
        };

        // 连接 MQTT 服务器
        const client = mqtt.connect(host, options);

        // 如果连接 MQTT 服务器成功
        client.on('connect', function() {
            console.log('Connected');
            document.getElementById('connectionStatus').classList.add('status-connected');
            document.getElementById('connectionStatus').classList.remove('status-disconnected');

            // 订阅传感器主题
            client.subscribe(deviceId + '/sensor/+', function(err) {
                if (!err) {
                    console.log('订阅主题: sensor');
                }
            });

            // 订阅设备状态主题
            client.subscribe(deviceId + '/state/+', function(err) {
                if (!err) {
                    console.log('订阅主题: state');
                }
            });
        });

        client.on('reconnect', function() {
            document.getElementById('connectionStatus').classList.remove('status-connected');
            document.getElementById('connectionStatus').classList.add('status-disconnected');
        });

        client.on('error', function(err) {
            console.log('Connection error: ', err);
            document.getElementById('connectionStatus').classList.remove('status-connected');
            document.getElementById('connectionStatus').classList.add('status-disconnected');
        });

        client.on('offline', function() {
            document.getElementById('connectionStatus').classList.remove('status-connected');
            document.getElementById('connectionStatus').classList.add('status-disconnected');
        });

        // 接收服务器消息
        client.on('message', function(topic, message) {
            if (topic === deviceId + '/sensor/dht11') {
                const data = message.toString();
                // 温度湿度数据是以下划线分隔的字符串，例如 "25.3_60"
                const parts = data.split('_');
                if (parts.length === 2) {
                    const temperature = parts[0];
                    const humidity = parts[1];
                    
                    const temperatureElement = document.getElementById('temperature');
                    temperatureElement.innerText = temperature;
                    temperatureElement.classList.remove('loading');
                    
                    // 温度超过30度时显示为红色
                    if (parseFloat(temperature) > 30) {
                        temperatureElement.classList.add('temperature-high');
                        document.getElementById('tempStatus').className = 'status-dot status-danger';
                        document.getElementById('tempStatusText').textContent = '温度过高';
                    } else {
                        temperatureElement.classList.remove('temperature-high');
                        document.getElementById('tempStatus').className = 'status-dot status-normal';
                        document.getElementById('tempStatusText').textContent = '温度正常';
                    }
                    
                    const humidityElement = document.getElementById('humidity');
                    humidityElement.innerText = humidity;
                    humidityElement.classList.remove('loading');
                    
                    // 湿度状态指示
                    if (parseFloat(humidity) > 70) {
                        document.getElementById('humidStatus').className = 'status-dot status-warning';
                        document.getElementById('humidStatusText').textContent = '湿度过高';
                    } else if (parseFloat(humidity) < 30) {
                        document.getElementById('humidStatus').className = 'status-dot status-warning';
                        document.getElementById('humidStatusText').textContent = '湿度过低';
                    } else {
                        document.getElementById('humidStatus').className = 'status-dot status-normal';
                        document.getElementById('humidStatusText').textContent = '湿度正常';
                    }
                }
            }
            else if(topic === deviceId + '/state/led') {
                // 处理 LED 状态更新
                const lightButton = document.getElementById('LightOn');
                if(message.toString() === '1') {
                    lightButton.innerHTML = '<i class="fas fa-lightbulb"></i> 关 灯';
                    lightButton.style.background = 'linear-gradient(135deg, #ff9800 0%, #f57c00 100%)';
                } else {
                    lightButton.innerHTML = '<i class="fas fa-lightbulb"></i> 开 灯';
                    lightButton.style.background = 'linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%)';
                }
            }
            else if(topic === deviceId + '/state/beep') {
                // 处理报警状态更新
                const beepButton = document.getElementById('BeepOn');
                if(message.toString() === '1') {
                    beepButton.innerHTML = '<i class="fas fa-bell-slash"></i> 静 音';
                    beepButton.style.background = 'linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%)';
                } else {
                    beepButton.innerHTML = '<i class="fas fa-bell"></i> 报 警';
                    beepButton.style.background = 'linear-gradient(135deg, var(--warning) 0%, #ff6d00 100%)';
                }                    
            }
        });

        // 按钮事件
        document.getElementById('LightOn').addEventListener('click', function() {
            var message = 'b';  // 关灯命令
            
            if(this.innerText === '开 灯') {
                message = 'a'; // 开灯命令
            }

            client.publish(deviceId + '/cmd', message);
        });

        document.getElementById('BeepOn').addEventListener('click', function() {
            var message = 'd'; // 静音命令
            if(this.innerText === '报 警') {
                message = 'c'; // 报警命令
            }

            client.publish(deviceId + '/cmd', message);
        });
    </script>
</body>
</html>